// Created, updated and maintained by Taufik Nurrohman.
// URL: https://plus.google.com/108949996304093815163/about

// Cookie
function createCookie(a,b,c){if(c){var d=new Date();d.setTime(d.getTime()+(c*24*60*60*1000));var e="; expires="+d.toGMTString()}else{var e=""}document.cookie=a+"="+b+e+"; path=/"}function readCookie(a){var b=a+"=";var d=document.cookie.split(';');for(var i=0;i<d.length;i++){var c=d[i];while(c.charAt(0)==' ')c=c.substring(1,c.length);if(c.indexOf(b)==0)return c.substring(b.length,c.length)}return null}function eraseCookie(a){createCookie(a,"",-1)}

(function($) {

	var $addToCart = $('.add-to-cart'),
		$emptyCart = $('.empty-cart'),
		$checkOut = $('.checkout'),
		$shoppingCart = $('.shopping-cart'),
		$ct = $shoppingCart.find('.content'),
		$total = $shoppingCart.find('.total'),
		name = (readCookie("name")) ? readCookie("name").split('+=') : [],
		price = (readCookie("price")) ? readCookie("price").split('+=') : [],
		thumb = (readCookie("thumb")) ? readCookie("thumb").split('+=') : [],
		data = (readCookie("data")) ? readCookie("data").split('+=') : [],
		scc = myShoppingCartConfig;

	$total.html(scc.totalText + scc.currency + ' 0');

	function addCommas(nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + '.' + '$2');
		}
		return x1 + x2;
	}

	function addToCart() {
		var sk = "", total = 0;
		sk = '<ol>';
		for (var i = 0, pl = price.length; i < pl; i++) {
			sk += '<li class="clearfix" title="' + name[i] + ' (' + price[i] + ')"><img src="' + thumb[i] + ' width="20" height="20">' + name[i] + ' (' + price[i] + ')<a href="#">&#215;</a></li>';
			total = total + parseInt(data[i], 10);
		}
		sk += '</ol>';
		$ct.html(sk);
		$total.html(scc.totalText + scc.currency + addCommas(total));
		createCookie("name", name.join('+='), scc.expires);
		createCookie("price", price.join('+='), scc.expires);
		createCookie("thumb", thumb.join('+='), scc.expires);
		createCookie("data", data.join('+='), scc.expires);
	}

	if (readCookie("name") && readCookie("price") && readCookie("thumb") && readCookie("data")) {
		addToCart();
	}

	$addToCart.on("click", function() {
		var tp = $(this).closest('.the-product'),
			pt = tp.find('.product-title').data("title"),
			pi = tp.find('img').first().attr('src').replace(/\/s[0-9]+\//, "/s20-c/"),
			pr = tp.find('.product-price').text(),
			dt = tp.find('.product-price').data('price');
		name.unshift(pt);
		price.unshift(pr);
		thumb.unshift(pi);
		data.unshift(dt);
		addToCart();
		return false;
	});

	$emptyCart.on("click", function() {
		$ct.html('');
		$total.html(scc.totalText + scc.currency + ' 0');
		name = [];
		price = [];
		thumb = [];
		data = [];
		eraseCookie("name");
		eraseCookie("price");
		eraseCookie("thumb");
		eraseCookie("data");
		return false;
	});

	$shoppingCart.on("click", "li a", function() {
		var index = $(this).parent().index();
		name.splice(index,1);
		price.splice(index,1);
		thumb.splice(index,1);
		data.splice(index,1);
		$(this).parent().fadeOut('fast', addToCart);
		return false;
	});

	// Order...
	var $orderTable = $('#order-table'),
		$orderCopy = $('#order-copy'),
		$orderBtn = $('.order-now');
	if (readCookie("name") && readCookie("price") && readCookie("thumb") && readCookie("data")) {
		var name = readCookie("name").split('+='),
			price = readCookie("price").split('+='),
			data = readCookie("data").split('+='),
			b = "",
			c = scc.orderTable,
			total = 0,
			d = "";
		b = '<tr><th>'+c.productTotal+'</th><th>'+c.productName+'</th></tr>';
		b += '<tr><td class="total-product">'+data.length+'</td><td><ol>';
		for (var i = 0, len = data.length; i < len; i++) {
			total = total + parseInt(data[i],10);
			b += '<li>'+name[i]+' ('+price[i]+')</li>';
			d += '    '+name[i]+' ('+price[i]+')\n';
		}
		b += '</ol></td></tr><tr><th>'+c.productPrice+'</th><td>'+scc.currency+addCommas(total)+'</td></td></tr>';
		$orderTable.html(b);
		$orderCopy.val(c.productName+' ('+data.length+'):\n'+d+'\n'+c.productPrice+':\n    '+scc.currency+addCommas(total));
	}
	$orderBtn.show().on("click", function() {
		$(this).hide().next().fadeIn('slow', function() {
			$(this).find('textarea').trigger("select");
		});
		return false;
	});

})(jQuery);